package com.aube.rvi.util;

import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.provider.Settings;
import android.telephony.TelephonyManager;

/**
 * Created by huyaonan on 16/10/21.
 */
public class AppUtil {
    public static String getIMEI(Context context) {
        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        String imei = tm.getDeviceId();
        // no sim: sdk|any pad
        if (null != imei && !Constant.SPECIAL_IMEI.equals(imei)) {
            Utils.Log("requestCount", "imei:" + imei);
            return imei;
        } else {
            String deviceId = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
            // sdk: android_id
            if (null != deviceId) {
                Utils.Log("requestCount", "ANDROID_ID:" + deviceId);
                return deviceId;
            }
        }
        return "";
    }

    /**
     * 获取手机的设备号或wifi的mac号，在wifi环境下只返回mac地址，否则返回手机设备号<br>
     * 在模拟器情况下会返回null
     *
     * @return
     */
    public static String getDeviceId(Context context) {
        WifiManager manager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        WifiInfo wifiInfo = manager.getConnectionInfo();
        String macAddress = wifiInfo.getMacAddress();
        if (null != macAddress) {
            Utils.Log("requestCount", "mac:" + macAddress);
            return macAddress.replace(".", "").replace(":", "")
                    .replace("-", "").replace("_", "").replace("+", "").replace("-", "").replace("=", "");
        } else {
            TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            String imei = tm.getDeviceId();
            // no sim: sdk|any pad
            if (null != imei && !Constant.SPECIAL_IMEI.equals(imei)) {
                Utils.Log("requestCount", "imei:" + imei);
                return imei;
            } else {
                String deviceId = Settings.Secure.getString(context.getContentResolver(),
                        Settings.Secure.ANDROID_ID);
                // sdk: android_id
                if (null != deviceId) {
                    Utils.Log("requestCount", "ANDROID_ID:" + deviceId);
                    return deviceId.replace("+", "").replace("-", "").replace("=", "");
                }
                return null;
            }
        }
    }
}
